package com.dmcliver.timetracker.session;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

import com.dmcliver.timetracker.persistence.PersonDAO;
import com.dmcliver.timetracker.service.StandardHashService;

@Name("authenticator")
public class Authenticator{
	
	@Logger private Log log;

    @In Identity identity;
    @In Credentials credentials; 
    @In PersonDAO personDAO;
    @In StandardHashService standardHashService;
    
    public  boolean authenticate(){
    	
        String username = credentials.getUsername();
		String password = credentials.getPassword();

        String hash = standardHashService.hash(username + password);
        boolean userExists = personDAO.exists(username, hash);
        
		if (userExists){
            identity.addRole("admin");
            return true;
        }
        return false;
    }
}
